<?php

class Admin_RoleController extends Zend_Controller_Action
{
	private $_auth;
	private $_acl;
    public function init(){
        $this->_params      = $this->_request->getParams();
        
        $this->_acl         = new My_Acl();
        $this->view->acl    = $this->_acl;
        
        $this->_auth        = $this->_acl->checkAllowed();
        $this->view->auth   = $this->_auth;
    }
    public function indexAction()
    {
        $modelRole          = new Admin_Model_Role();
        $roleList           = $modelRole->getAllData()->toArray();
        $this->view->roleList = $roleList;
    }
    public function deleteAction()
    {
        if($this->getRequest()->isPost()){
            $del = $this->getRequest()->getPost('del');
            if($del == 'Có'){
                $roleId = $this->getRequest()->getPost('roleId');
                $modelRole = new Admin_Model_Role();
                $roleCheck = $modelRole->getDataByRoleId($roleId);
                $modelRole->deleteData($roleId);
                $modelUserrole = new Admin_Model_Userrole();
                $modelUserrole->deleteDataByRoleId($roleId);
                // ghi log
            }
            $this->_helper->redirector('index', 'role', 'admin');
        }else{
            $id = $this->_getParam('id', 0);
            $modelRole = new Admin_Model_Role();
            $this->view->roleCheck = $modelRole->getDataByRoleId($id);
        }
    }
   public function addAction()
    {
        $configApp = new Zend_Config_Ini(CONFIG_PATH . '/application.ini', APPLICATION_ENV);
        $this->view->configApp = $configApp;
        if($this->getRequest()->isPost()){
            $formData = $this->getRequest()->getPost();
            if($formData){
              
                $resourceId = '';
               
                if(isset($formData['roleName']))
                    $roleName = $formData['roleName'];
                
                if(isset($formData['resourceId']))
                    $resourceId = $formData['resourceId'];
               
                
                if($roleName){
                    $modelRole = new Admin_Model_Role();
                    $roleCheck_name = $modelRole->getDataByRoleName($roleName);
                    if(!$roleCheck_name){
                        $roleId = $modelRole->addData($roleName, json_encode($resourceId));
                        if($roleId){
                            $this->_helper->redirector('index', 'role', 'admin');
                        }else{
                            $this->view->mess = "Đã có lỗi xảy ra. Vui lòng thử lại!";
                            $this->view->flag = "error";
                       }
                    }else{
                        $this->view->mess = "Tên quyền đã tồn tại. Vui lòng chọn tên khác!";
                        $this->view->flag = "error";
                    }
                }else{
                    $this->view->mess = "Vui lòng điền đẩy đủ thông tin!";
                    $this->view->flag = "error";
                }
            }else{
                $this->_helper->redirector('index', 'role', 'admin');
            }
        }
        
        // mảng tất cả resource
        $modelResource  = new Admin_Model_Resource();
        $resourceList   = $modelResource->getAllData();
        $this->view->resourceList = $resourceList;
        
    }
    
    public function editAction()
    {
        $configApp = new Zend_Config_Ini(CONFIG_PATH . '/application.ini', APPLICATION_ENV);
        $this->view->configApp = $configApp;
        $roleId = (int)($this->_getParam('id', 0));
        $modelRole = new Admin_Model_Role();
        $roleCheck = $modelRole->getDataByRoleId($roleId);
        $this->view->roleCheck = $roleCheck;
        if($roleCheck){
            if($this->getRequest()->isPost()){
                $formData = $this->getRequest()->getPost();
                if($formData){
                    $dataUpdate['resourceId'] = '';
                    if(isset($formData['roleName']))
                        $roleName = $formData['roleName'];
                    if(isset($formData['resourceId']))
                        $dataUpdate['resourceId'] = json_encode($formData['resourceId']);
                    if($roleName){
                        $roleCheck_name = $modelRole->getDataByRoleName($roleName);
                        if(!$roleCheck_name || $roleCheck_name['roleId'] == $roleCheck['roleId']){
                            $modelRole->updateData($roleId, $roleName, $dataUpdate['resourceId']);
                           
                            $this->_helper->redirector('index', 'role', 'admin');
                        }else{
                            $this->view->mess = "Tên quyền đã tồn tại. Vui lòng chọn tên khác!";
                            $this->view->flag = "error";
                        }
                    }else{
                        $this->view->mess = "Vui lòng điền đẩy đủ thông tin!";
                        $this->view->flag = "error";
                    }
                }else{
                    $this->_helper->redirector('index', 'role', 'admin');
                }
            }else{
                // mảng tất cả resource
                $modelResource  = new Admin_Model_Resource();
                $resourceList   = $modelResource->getAllData();
                $this->view->resourceList = $resourceList;
            }
        }else{
            $this->_helper->redirector('index', 'role', 'admin');
        }
    }
}







